package com.kede.app.service.impl;

import com.kede.app.domain.Package;
import com.kede.app.repository.PackageRepository;
import com.kede.app.service.PackageService;
import com.kede.app.service.dto.PackageDto;
import com.kede.app.service.dto.PackageQueryCriteria;
import com.kede.app.service.mapstruct.PackageMapper;
import com.kede.utils.FileUtil;
import com.kede.utils.PageUtil;
import com.kede.utils.QueryHelp;
import com.kede.utils.ValidationUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
* @description 服务实现
* @author Pr.li
* @date 2023-10-14
**/
@Service
@RequiredArgsConstructor
public class PackageServiceImpl implements PackageService {

    private final PackageRepository packageRepository;
    private final PackageMapper packageMapper;

    @Override
    public Map<String,Object> queryAll(PackageQueryCriteria criteria, Pageable pageable){
        Page<Package> page = packageRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(packageMapper::toDto));
    }

    @Override
    public List<PackageDto> queryAll(PackageQueryCriteria criteria){
        return packageMapper.toDto(packageRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public PackageDto findById(Integer id) {
        Package packagea = packageRepository.findById(id).orElseGet(Package::new);
        ValidationUtil.isNull(packagea.getId(),"Package","id",id);
        return packageMapper.toDto(packagea);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public PackageDto create(Package resources) {
        return packageMapper.toDto(packageRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(Package resources) {
        Package packagea = packageRepository.findById(resources.getId()).orElseGet(Package::new);
        ValidationUtil.isNull( packagea.getId(),"Package","id",resources.getId());
        packagea.copy(resources);
        packageRepository.save(packagea);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            packageRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<PackageDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (PackageDto packagea : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put(" goodIds",  packagea.getGoodIds());
            map.put("门店编号", packagea.getStoreId());
            map.put("区代编号", packagea.getQuId());
            map.put("市代编号", packagea.getShiId());
            map.put(" cateName",  packagea.getCateName());
            map.put("包含的个数", packagea.getCount());
            map.put("门店名称", packagea.getStoreName());
            map.put("区代名称", packagea.getQuName());
            map.put("市代名称", packagea.getShiName());
            map.put("门店打包时间", packagea.getStoreCreateTime());
            map.put("包裹状态 1正常 2待区代审核 3区代正常 4市代审核 5市代正常 6运输 7已分拣8区代拒绝9市代拒绝", packagea.getStatus());
            map.put("区代打包时间", packagea.getQuCreateTime());
            map.put("市代打包时间", packagea.getShiCreateTime());
            map.put("运输编号", packagea.getTransportNo());
            map.put("原因说明", packagea.getReason());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}